package com.dragon.graduation.teacher.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.graduation.user.service.UserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.dragon.util.MyPage;
import com.dragon.util.WrapperUtil;
import com.dragon.graduation.teacher.controller.dto.TeacherAddDTO;
import com.dragon.graduation.teacher.controller.dto.TeacherSearchDTO;
import com.dragon.graduation.teacher.controller.dto.TeacherUpdateDTO;
import com.dragon.graduation.teacher.controller.vo.TeacherVO;
import com.dragon.graduation.teacher.entity.TeacherEntity;
import com.dragon.graduation.teacher.service.TeacherService;
import com.dragon.config.exception.exception.GlobalException;
import com.dragon.results.page.ResultPage;
import com.dragon.results.result.HttpResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 教师表 前端控制器
 * </p>
 *
 * @author dragon
 * @since 2025-02-28 21:24:33
 */
@RestController
@RequestMapping("/teacher")
@RequiredArgsConstructor
@Api(tags = "教师表 相关接口")
public class TeacherController {

    private final TeacherService teacherService;
    private final UserService userService;

    @PostMapping("add")
    @ApiOperation("1]教师表添加")
    @ApiOperationSupport(order = 1)
    public HttpResult<String> add(@RequestBody TeacherAddDTO addDTO) {
        TeacherEntity entity = TeacherAddDTO.to(addDTO, TeacherEntity.class);

        //新增账号
        Integer userId = userService.addUser(entity.getEmployeeNumber(), entity.getName(), 3);
        entity.setUserId(userId);
        teacherService.save(entity);
        return HttpResult.ok("添加成功");
    }

    @PostMapping("update")
    @ApiOperation("2]教师表修改")
    @ApiOperationSupport(order = 2)
    public HttpResult<String> update(@RequestBody TeacherUpdateDTO updateDTO) {
        TeacherEntity entity = teacherService.getById(updateDTO.getId());
        Assert.isFalse(ObjectUtils.isEmpty(entity), () -> new GlobalException("要修改的数据不存在"));
        entity = TeacherUpdateDTO.to(updateDTO, TeacherEntity.class);
        teacherService.updateById(entity);
        return HttpResult.ok("修改成功");
    }

    @PostMapping("search")
    @ApiOperation("3]教师表分页查询")
    @ApiOperationSupport(order = 3)
    public HttpResult<ResultPage<List<TeacherVO>>> search(@RequestBody TeacherSearchDTO searchDTO) {
        QueryWrapper<TeacherEntity> queryWrapper = WrapperUtil.constructWrapper(searchDTO, TeacherEntity.class);

        Page<TeacherEntity> entityPage = MyPage.setPage(TeacherEntity.class, searchDTO.getPage());
        Page<TeacherEntity> page = teacherService.page(entityPage, queryWrapper);
        ResultPage<List<TeacherVO>> resultPage = MyPage.copyPage(page, TeacherVO.class);
        return HttpResult.okForData(resultPage);
    }

    @PostMapping("delete")
    @ApiOperation("4]教师表删除")
    @ApiOperationSupport(order = 4)
    public HttpResult<String> delete(@RequestBody List<Integer> idList) {
        teacherService.removeByIds(idList);
        userService.removeByUserIds(idList);
        return HttpResult.ok("删除成功");
    }
}
